﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormSupplyAutoConn : System.Windows.Forms.Form
	{
		public FormSupplyAutoConn()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = new HVACUserSetting();
			this.SetInitialInterface();
		}

		private void FormSupplyAutoConn_Load(object sender, EventArgs e)
		{
		}

		private void SetInitialInterface()
		{
			SupplyWaterConn supplyWaterConn = this.userSetting.GetSupplyWaterConn();
			if (supplyWaterConn.ConnStyle)
			{
				this.rdoConnRiser.Select();
				this.rdoConnRiser.Checked = true;
			}
			else
			{
				this.rdoConnHorizon.Select();
				this.rdoConnHorizon.Checked = true;
			}
			this.txtAngle.Text = supplyWaterConn.Angle;
			this.txtMainPipeOffset.Text = supplyWaterConn.MainPipeOffset;
			this.txtValveOffset.Text = supplyWaterConn.ValveOffset;
			this.txtDiameter.Text = supplyWaterConn.Diameter;
			if (supplyWaterConn.WaterMeter)
			{
				this.ckbWaterMeter.Checked = true;
			}
			else
			{
				this.ckbWaterMeter.Checked = false;
			}
			if (supplyWaterConn.BeDraw)
			{
				this.ckbDraw.Checked = true;
			}
			else
			{
				this.ckbDraw.Checked = false;
			}
			this.cbxLinkBathe.Checked = supplyWaterConn.BLinkBathe;
			if (this.rdoConnRiser.Checked)
			{
				this.txtAngle.Enabled = true;
				this.txtDiameter.Enabled = true;
				this.btnPick.Enabled = true;
			}
			if (this.rdoConnHorizon.Checked)
			{
				if (this.ckbDraw.Checked)
				{
					this.txtAngle.Enabled = true;
					this.txtDiameter.Enabled = true;
					this.btnPick.Enabled = true;
				}
				else
				{
					this.txtAngle.Enabled = false;
					this.txtDiameter.Enabled = false;
					this.btnPick.Enabled = false;
				}
			}
			if (supplyWaterConn.BIsParallel)
			{
				this.bIsParallel = true;
				this.SelectButton(this.bntParallel);
				return;
			}
			this.bIsParallel = false;
			this.SelectButton(this.bntVertical);
		}

		private void btnPick_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Abort;
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			this.SaveInterFace();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void ValidatingText(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			if (hytextBox.Name == "txtAngle")
			{
				double num = Convert.ToDouble(hytextBox.Text);
				if (num >= 360.0 || num < 0.0)
				{
					YJKMessageBox.Warning("输入角度应在0-360度之间。");
					hytextBox.SelectAll();
					e.Cancel = true;
					return;
				}
			}
			else if (hytextBox.Name == "txtDiameter")
			{
				int num2 = Convert.ToInt32(hytextBox.Text);
				if (num2 >= 1000 || num2 < 15)
				{
					YJKMessageBox.Warning("管径规格应在15-1000mm之间。");
					hytextBox.SelectAll();
					e.Cancel = true;
					return;
				}
			}
			else if (hytextBox.Name == "txtMainPipeOffset" || hytextBox.Name == "txtValveOffset")
			{
				double num3 = Convert.ToDouble(hytextBox.Text);
				if (num3 >= 200.0 || num3 < 0.0)
				{
					YJKMessageBox.Warning("相对标高设置应该在0-200m之间。");
					hytextBox.SelectAll();
					e.Cancel = true;
				}
			}
		}

		private void BindingPipeSize(Pipe pipe)
		{
		}

		private void SelectRadioButton()
		{
			if (this.rdoConnRiser.Checked)
			{
				this.txtMainPipeOffset.Enabled = true;
				this.txtAngle.Enabled = true;
				this.txtDiameter.Enabled = true;
				this.ckbWaterMeter.Enabled = true;
				this.btnPick.Enabled = true;
				this.ckbDraw.Enabled = false;
				return;
			}
			this.txtMainPipeOffset.Enabled = false;
			this.txtAngle.Enabled = false;
			this.txtDiameter.Enabled = false;
			this.ckbWaterMeter.Enabled = false;
			this.btnPick.Enabled = false;
			this.ckbDraw.Enabled = true;
		}

		private void rdoConnHorizon_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectRadioButton();
			this.ChangeState();
		}

		public void SetAngle(double angle)
		{
			this.txtAngle.Text = angle.ToString();
		}

		public double GetAngle()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.txtAngle.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public bool GetDesignStyle()
		{
			return this.rdoConnRiser.Checked;
		}

		public bool GetBeDrawPipe()
		{
			return this.ckbDraw.Checked;
		}

		public bool GetWaterMeter()
		{
			return this.ckbWaterMeter.Checked;
		}

		public bool IsLinkBathe()
		{
			return this.cbxLinkBathe.Checked;
		}

		public double GetPipeDiameter()
		{
			double result = 100.0;
			try
			{
				result = Convert.ToDouble(this.txtDiameter.Text);
			}
			catch (Exception)
			{
				result = 100.0;
				throw;
			}
			return result;
		}

		public double GetPipeOffset()
		{
			return Convert.ToDouble(this.txtMainPipeOffset.Text) * 1000.0;
		}

		private void ckbDraw_CheckedChanged(object sender, EventArgs e)
		{
			if (!this.ckbDraw.Enabled)
			{
				return;
			}
			this.ChangeState();
		}

		private void ChangeState()
		{
			if (this.rdoConnRiser.Checked || this.ckbDraw.Checked)
			{
				this.txtAngle.Enabled = true;
				this.txtDiameter.Enabled = true;
				this.btnPick.Enabled = true;
				return;
			}
			this.txtAngle.Enabled = false;
			this.txtDiameter.Enabled = false;
			this.btnPick.Enabled = false;
		}

		public double GetValveOffset()
		{
			return Convert.ToDouble(this.txtValveOffset.Text);
		}

		private void SaveInterFace()
		{
			SupplyWaterConn supplyWaterConn = new SupplyWaterConn();
			supplyWaterConn.ConnStyle = this.rdoConnRiser.Checked;
			supplyWaterConn.BeDraw = this.ckbDraw.Checked;
			supplyWaterConn.WaterMeter = this.ckbWaterMeter.Checked;
			supplyWaterConn.Angle = this.txtAngle.Text;
			supplyWaterConn.Diameter = this.txtDiameter.Text;
			supplyWaterConn.MainPipeOffset = this.txtMainPipeOffset.Text;
			supplyWaterConn.ValveOffset = this.txtValveOffset.Text;
			supplyWaterConn.BLinkBathe = this.cbxLinkBathe.Checked;
			supplyWaterConn.BIsParallel = this.bIsParallel;
			this.userSetting.WriteSupplyWaterConn(supplyWaterConn);
		}

		private void btn_Click(object sender, EventArgs e)
		{
			System.Drawing.Color backColor = this.gbStyle.BackColor;
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			string name = button.Name;
			if (!(name == "bntParallel"))
			{
				if (name == "bntVertical")
				{
					this.bIsParallel = false;
				}
			}
			else
			{
				this.bIsParallel = true;
			}
			foreach (object obj in this.gbStyle.Controls)
			{
				Button button2 = obj as Button;
				if (button2 != null)
				{
					button2.BackColor = backColor;
					if (button2 == button)
					{
						button2.BackColor = System.Drawing.Color.LightSkyBlue;
					}
				}
			}
		}

		private void SelectButton(Button button)
		{
			this.btn_Click(button, null);
		}

		public bool bIsParallel = true;

		private HVACUserSetting userSetting;
	}
}
